package com.yqb.study.modular.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yqb.study.annotation.multidatasource.DataSourceAnt;
import com.yqb.study.constant.multidatasource.DataSourceEnum;
import com.yqb.study.modular.dao.mysql.CityDao;
import com.yqb.study.modular.persistence.dao.CityMapper;
import com.yqb.study.modular.persistence.model.City;
import com.yqb.study.modular.service.ICityService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CityServiceImpl implements ICityService {
	
	@Autowired
	private CityMapper cityMapper;
	@Autowired
	private CityDao cityDao; 	

	@Override
	public List<City> getAllCityInfo() {
        Wrapper<City> cityWrapper = new EntityWrapper<>();
//        cityWrapper = cityWrapper.eq("id", 2);		
		List<City> cityList = cityMapper.selectList(cityWrapper);
		
		for (City cityItem : cityList) {
			log.info(cityItem.toString());
		}
		
		return cityList;
	}
	
	@Override
	public List<City> getCityInfoWith(int id) {
        Wrapper<City> cityWrapper = new EntityWrapper<>();
        cityWrapper = cityWrapper.eq("id", id);		
		List<City> cityList = cityMapper.selectList(cityWrapper);
		
		for (City cityItem : cityList) {
			log.info(cityItem.toString());
		}
		
		return cityList;
	}	

	@Override
	@DataSourceAnt(dataSourceEnum = DataSourceEnum.MASTERDB)
	public void getAllCityInfoFromMasterDb() {
        Wrapper<City> cityWrapper = new EntityWrapper<>();
//        cityWrapper = cityWrapper.eq("id", 2);		
        List<City> cityList = cityMapper.selectList(cityWrapper);
		
		for (City cityItem : cityList) {
			log.info(cityItem.toString());
		}		
	}

	@Override
	@DataSourceAnt(dataSourceEnum = DataSourceEnum.CLUSTERDB)
	public void getAllCityInfoFromClusterDb() {
        Wrapper<City> cityWrapper = new EntityWrapper<>();
//        cityWrapper = cityWrapper.eq("id", 2);		
        List<City> cityList = cityMapper.selectList(cityWrapper);
		
		for (City cityItem : cityList) {
			log.info(cityItem.toString());
		}		
	}

	@Override
	@DataSourceAnt(dataSourceEnum = DataSourceEnum.MASTERDB)
	@Transactional
	public void saveCityListInMasterDb(List<City> cityList) {
		for (City cityItem : cityList) {
			cityMapper.insert(cityItem);
		}
	}

	@Override
	@DataSourceAnt(dataSourceEnum = DataSourceEnum.CLUSTERDB)
	public void saveCityListInClusterDb(List<City> cityList) {
		for (City cityItem : cityList) {
			cityMapper.insert(cityItem);
		}
	}

	@Override
	@DataSourceAnt(dataSourceEnum = DataSourceEnum.CLUSTERDB)
	public List<City> getPageCityInfoFromClusterDb(Page<City> page, Map<String, String> searchCond) {		
		return cityDao.getPageCityInfo(page, searchCond);
	}

	@Override
	@DataSourceAnt(dataSourceEnum = DataSourceEnum.CLUSTERDB)
	public List<Map<String, Object>> getPageCityInfoMapFromClusterDb(Page<Map<String, Object>> page,
			Map<String, String> searchCond) {
		return cityDao.getPageCityInfoMap(page, searchCond);
	}

	@Override
	public int deleteCityInfo(String cityName) {
        Wrapper<City> cityWrapper = new EntityWrapper<>();
        cityWrapper = cityWrapper.eq("city_name", cityName);		
      
		return cityMapper.delete(cityWrapper);
	}

	@Override
	public int updateCityInfo(City city) {
		return cityMapper.updateById(city);
	}
}
